package jltx.fragmentpageframework.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.TextView;

import cn.jltx.utils.ViewHelper;
import jltx.fragmentpageframework.R;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/06/21.
 */

public class BottomBarTabV2 extends FrameLayout{
    private ChangeColorImageTextView mIcon;
    private TextView mTvUnreadCount;
    private int mTabPosition = -1;

    public BottomBarTabV2(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public BottomBarTabV2(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public BottomBarTabV2(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public void initView(Context context){
        LayoutInflater.from(context).inflate(R.layout.bottom_tab2,this);
        TypedValue typedValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);
        int[] attribute = new int[]{android.R.attr.selectableItemBackground};
        TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(typedValue.resourceId, attribute);
        this.setBackground(typedArray.getDrawable(0));
        typedArray.recycle();
        mIcon = ViewHelper.findViewById(this,R.id.bottom_tab_icon);
        mTvUnreadCount = ViewHelper.findViewById(this,R.id.bottom_tab_unread_tv);
    }

    public BottomBarTabV2 setIconBottomTextColor(@ColorRes int unselectColor, @ColorRes int selectColor) {
        mIcon.setIconBottomTextColor(unselectColor,selectColor);
        return this;
    }

    public BottomBarTabV2 setIconMaskDrawable(@DrawableRes int resUnselect, @DrawableRes int resSelect){
        mIcon.setIconMaskDrawable(resUnselect,resSelect);
        return this;
    }

    public BottomBarTabV2 setIconBottomText(String text){
        mIcon.setIconBottomText(text);
        return this;
    }


    public void changeBottomBatTabColor(float offset){
        mIcon.changeImageViewDrawable(offset);
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected) {
            changeBottomBatTabColor(1f);
        } else {
            changeBottomBatTabColor(0f);
        }
    }

    public void setTabPosition(int position) {
        mTabPosition = position;
    }

    public int getTabPosition() {
        return mTabPosition;
    }

}
